package com.zhida.jiyangcenter.setting

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.resColor
import chooongg.base.utils.tipFail
import chooongg.base.utils.tipSuccess
import chooongg.base.utils.toast
import chooongg.http.APIException
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_nikename_edit.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@TitleBar(false)
@ContentLayout(R.layout.activity_nikename_edit)
class EditNikeNameActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }.title(intent.getStringExtra("title")).operationText("提交") {
            commit(edit_text.text.toString())
        }.getOperationTextView().apply {
            setTextColor(resColor(R.color.white))
        }
        edit_text.setText(intent.getStringExtra("context"))
    }

    private fun commit(name: String) {
        when {
            name.isEmpty() -> when {
                intent.getStringExtra("type") == "name" -> toast("请填写您的真实姓名")
                intent.getStringExtra("type") == "nickname" -> toast("请填写您的昵称")
                intent.getStringExtra("type") == "wechat" -> edit_text.hint = "请填写您的微信账号"
                intent.getStringExtra("type") == "alipay" -> edit_text.hint = "请填写您的支付宝账号"
            }
            name == intent.getStringExtra("name") -> when {
                intent.getStringExtra("type") == "name" -> toast("您的真实姓名未做修改")
                intent.getStringExtra("type") == "nickname" -> toast("您的昵称未做修改")
                intent.getStringExtra("type") == "wechat" -> edit_text.hint = "您的微信账号未做修改"
                intent.getStringExtra("type") == "alipay" -> edit_text.hint = "您的支付宝账号未做修改"
            }
            else -> {
                val queryMap = HashMap<String, Any?>().apply {
                    when {
                        intent.getStringExtra("type") == "name" -> put("user_name", name)
                        intent.getStringExtra("type") == "nickname" -> put("nick_name", name)
                        intent.getStringExtra("type") == "wechat" -> put("weixin_code", name)
                        intent.getStringExtra("type") == "alipay" -> put("zhifub_code", name)
                    }
                }
                showTipLoading("提交中")
                lifecycleScope.request({ APIAccount.service().nicknameAdd(name) },
                    object : BaseAPI<Any>() {
                        override fun onSuccess(t: Any?, state: Int, msg: String) {
                            tipSuccess("修改成功")
                            finish()
                        }

                        override fun onError(e: APIException) {
                            tipFail(e.eMessage)
                        }

                    })
            }
        }

    }
}